<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Logic;

/**
 * 服务类型业务逻辑层
 *
 * @author derek <693472824@qq.com>
 */
class ClassLogic extends BaseLogic {

    /**
     * 根据服务类型ID获取服务类型
     * @author  derek <693472824@qq.com>
     */
    public function getClassByID ($classID=0) {
        
        if(empty($classID) || !is_numeric($classID)){
            return null;
        }
        
        $classModel = M('Class');
        $map["class_id"] = (string)$classID;
        $class = $classModel->where($map)->find();
        return $class;
    }
    
    /**
     * 保存服务类型信息
     * @author derek <693472824@qq.com>
     */
    public function saveAction() {
        
        $classID = I('class_id');
        $classModel = D('Class');
        
        $data = I();
        $data['profession'] = is_array($data['skill']) ? implode(',', $data['skill']) : '';
    
        if ($classModel->create($data)) {
    
            $emergency = I('emergency');
            
            if (empty($classID)) {
                $classID = $classModel->add();
                if($classID){
                    M('ClassDetail')->where(array('class_id'=>$classID))->setField('emergency', $emergency);
                    return true;
                }else{
                    return false;
                }
            } else {
                $map['class_id'] = $classID;
                M('ClassDetail')->where($map)->setField('emergency', $emergency);
                return $classModel->where($map)->save();
            }
        } else {
            $this->error = $classModel->getError();
            return false;
        }
    }

    /**
     * 服务类型删除
     * @author  derek <693472824@qq.com>
     */
    public function del ($classID) {
        
        if(empty($classID)){
            return false;
        }
        
        $map['class_id'] = $classID;
        
        $result = M('Class')->where($map)->delete();
        
        return $result;
    }
    
    /**
     * 树形结构
     * @author  derek <693472824@qq.com>
     */
    public function getClassTree($parent_id = '0'){
        
        $tree = array();
        
        if (!is_numeric($parent_id)) {
            return $tree;
        }
        
        $list = M('Class')->select();
        
        $tree = api('Util/list_to_tree', array($list, 'class_id', 'parent_id', '_child', $parent_id));
        
        return $tree;
    }

    /**
     * 取细分项
     * @author  derek <693472824@qq.com>
     */
    public function getDetailByID ($detailID=0) {
    
        if(empty($detailID) || !is_numeric($detailID)){
            return null;
        }
    
        $detailModel = M('ClassDetail');
        $map['detail_id'] = (string)$detailID;
        $detail = $detailModel->where($map)->find();
        return $detail;
    }

    /**
     * 保存服务类型信息
     * @author derek <693472824@qq.com>
     */
    public function saveDetailAction() {
    
        $detailID = I('detail_id');
        $classID = I('class_id');
        $classModel = D('Class');
        $detailModel = D('ClassDetail');
        $model = M('ClassDetail');
    
        if ($detailModel->create()) {
    
            //查询同一服务类型下是否有非紧急服务，若存在非紧急服务，则父级服务类型 emergency(细分项是否全部为紧急服务）为2
            $classMap['class_id'] = $classID;
            $classMap['emergency'] = '2';
            $emergency = I('emergency');
            
            if ($emergency == '1'){
                $emergencyList = $model->where($classMap)->find();
                $emergency = empty($emergencyList) || count($emergencyList) < 1 ? '1' : '2';
            }else{
                $emergency = '2';
            }
            
            $classModel->where(array('class_id'=>$classID))->setField('emergency', $emergency);
            
            S('class_detail_list', NULL);
            
            if (empty($detailID)) {
                return $detailModel->add();
            } else {
                $map['detail_id'] = $detailID;
                return $detailModel->where($map)->save();
            }
        } else {
            $this->error = $detailModel->getError();
            return false;
        }
    }
    

    /**
     * 服务类型删除
     * @author  derek <693472824@qq.com>
     */
    public function delDetail ($detailID) {
    
        if(empty($detailID)){
            return false;
        }
    
        $map['detail_id'] = $detailID;
        $orderModel = M('Order')->where($map)->find();
        
        if(empty($orderModel)){
            $result = M('ClassDetail')->where($map)->delete();
            S('class_detail_list', NULL);
        }else{
            $result = '-1';
        }
    
        return $result;
    }
    
    /**
     * 取所有细分项
     */
    public function getDetailList(){
        
        $list = S('class_detail_list');
        
        if(empty($list)){
        
            $list = M('ClassDetail')->getField('detail_id,class_id,detail_name,price,unit,emergency,kwords');
        
            S('class_detail_list', $list);
        }
        
        return $list;
    }
}